// pages/home/home.js
Page({

    /**
     * 页面的初始数据
     * KEY 和 SECRET 需要配置 **************************
     */
    data: {
      APP_KEY: "8wjMTUANpNuSeAGC2bGA3yV9",
      APP_SECRET: "iWK476LwxF0As8dA8mvC6eKSiLMWCS98",
        token: "",
        height: 0,
        position: "back",
        src: "",
        userinfo: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        const sysinfo = wx.getSystemInfoSync()
            // console.log(sysinfo)
        this.setData({
            height: sysinfo.screenHeight
        })
    },
    reverse() {
        this.setData({
            position: this.data.position === "back" ? "front" : "back"
        });
    },
    takePhoto() {
        var that = this;
        const ctx = wx.createCameraContext()
        ctx.takePhoto({
            quality: 'high',
            success: (res) => {
                this.setData({
                    src: res.tempImagePath
                }, () => {
                    that.getFaceInfo();
                })
            }
        })
    },
    choosePhoto() {
        var that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original'],
            sourceType: ['album'],
            success(res) {
                // console.log(res);
                if (res.errMsg === "chooseImage:ok" &&
                    res.tempFilePaths.length > 0) {
                    that.setData({
                        src: res.tempFilePaths[0]
                    }, () => {
                        that.getFaceInfo();
                    })
                }
            }
        })
    },
    reChoose() {
        this.setData({
            src: "",
            userinfo: "",
            token: ""
        })
    },

    getFaceInfo() {
        var that = this;
        wx.request({
            url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + that.data.APP_KEY + "&client_secret=" + that.data.APP_SECRET,
            success: (res) => {
                // console.log(res);
                that.setData({
                        token: res.data.access_token
                    },
                    () => {
                        that.submitRequest();
                    })
            }
        })
    },
    submitRequest() {
        var that = this;
        const parms = {
            image: "",
            image_type: "BASE64",
            // face_field: "age,beauty,expression,gender,glasses,emotion"
        };
        const fileManager = wx.getFileSystemManager();
        fileManager.readFile({
            filePath: that.data.src,
            encoding: "base64",
            success: (res) => {
              //console.log(res.data);
                parms.image = res.data;
                wx.showLoading({
                    title: '正在识别。。。',
                });
                wx.request({
                  url: 'https://aip.baidubce.com/rest/2.0/image-classify/v1/animal?access_token=' + that.data.token,
                    method: "POST",
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded"
                    },
                    data: parms,
                    success: (res) => {
                      // 使用shift()可以获取数组第一条数据
                      console.log(res.data.result);
                         wx.hideLoading();
                         if (res.errMsg === "request:ok" && res.data.result !== null ) {
                           that.setData({
                             userinfo: res.data.result[0]
                             })
                         }
                    },
                })
            }
        })
    }

})